package com.sairui.xiaoruilibdemo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;

import com.sairui.xiaoruilib.XiaoRuiActivity;
import com.sairui.xiaoruilib.XiaoRuiLibSp;
import com.sairui.xiaoruilib.util.sp.PreferenceImpl;

/**
 * Created by sunqiyong on 2018/6/30.
 */

public class HomeFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {
    private View mRootView;
    private Switch aSwitch;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;

    public static HomeFragment getInstance(){
        return new HomeFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_home, null);
            initViews();
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    private void initViews() {
        aSwitch = mRootView.findViewById(R.id.switch_speak);
        aSwitch.setChecked(PreferenceImpl.getPreference(getActivity()).getBoolean(XiaoRuiLibSp.IS_CAN_SPEAK,true));


        radioButton1 = mRootView.findViewById(R.id.radio1);
        radioButton2 = mRootView.findViewById(R.id.radio2);
        radioButton3 = mRootView.findViewById(R.id.radio3);
        radioButton4 = mRootView.findViewById(R.id.radio4);

        switch (PreferenceImpl.getPreference(getActivity()).getInteger(XiaoRuiLibSp.IDENTIFY_SPEAK)){
            case 1:
                radioButton1.setChecked(true);
                break;
            case 2:
                radioButton2.setChecked(true);
                break;
            case 3:
                radioButton3.setChecked(true);
                break;
            case 4:
                radioButton4.setChecked(true);
                break;

        }



        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PreferenceImpl.getPreference(getActivity()).put(XiaoRuiLibSp.IS_CAN_SPEAK,isChecked);
            }
        });


        radioButton1.setOnCheckedChangeListener(this);
        radioButton2.setOnCheckedChangeListener(this);
        radioButton3.setOnCheckedChangeListener(this);
        radioButton4.setOnCheckedChangeListener(this);

        mRootView.findViewById(R.id.start_act).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(XiaoRuiActivity.getInstance(getActivity()));
            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            switch (buttonView.getId()) {
                case R.id.radio1:
                    PreferenceImpl.getPreference(getActivity()).put(XiaoRuiLibSp.IDENTIFY_SPEAK, 1);
                    break;
                case R.id.radio2:
                    PreferenceImpl.getPreference(getActivity()).put(XiaoRuiLibSp.IDENTIFY_SPEAK, 2);
                    break;
                case R.id.radio3:
                    PreferenceImpl.getPreference(getActivity()).put(XiaoRuiLibSp.IDENTIFY_SPEAK, 3);
                    break;
                case R.id.radio4:
                    PreferenceImpl.getPreference(getActivity()).put(XiaoRuiLibSp.IDENTIFY_SPEAK, 4);
                    break;
            }
        }
    }
}
